import axios from 'axios'
import {
    Loading,
    Notification,
    Message
} from 'element-ui';
let loadInstance;
import {
    getToken
} from '@/utils/getCookies'
const ajax = axios.create({
    baseURL: '/api',
    timeout: 60000
})
ajax.interceptors.request.use(
    config => {
        loadInstance = Loading.service({
            fullscreen: true
        });
        let token = process.env.NODE_ENV === 'production' ? '00000000' : '00000000000';
        if (token) {
            config.headers['t'] = token;
        } else {
            // token失效
        }
        config.data = config.data
        return config;
    },
    error => {
        loadInstance.close();
        console.log(error);
    }
)
ajax.interceptors.response.use(
    response => {
        loadInstance.close();
        let res = response.data;
        return res
    },
    error => {
        loadInstance.close();
        console.log(error);
    
        Message({
            message: error.message,
            type: 'error',
            duration:5*1000
          });
        return Promise.reject(error)
    }
    
)


//   ajax.interceptors.response.use(
//         response => {
//             const res = response.data;
//             if(response.statusText == 'OK'){
//                 return res
//             }else{
//                 if(response.status == 200){
//                     if(response.status != 1){
//                         this.$message({
//                             message: res.msg,
//                             type: 'error',
//                             duration:5*1000
//                           });
//                     }
//                     return res;
//                 }
//             }
    
          
           
//         },
//         error => {
//             console.log('err' + error)
//             this.$message({
//                 message: err.msg,
//                 type: 'error',
//                 duration:5*1000
//               });
//             return Promise.reject(error)
//         }
     
//     )
export default ajax